package org.dromara.hm.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.hm.domain.bo.HmSaleOrderRefundBo;
import org.dromara.hm.domain.vo.HmCustomerVo;
import org.dromara.hm.domain.vo.HmProductVo;
import org.dromara.hm.service.IHmCustomerService;
import org.dromara.hm.service.IHmProductService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderVo;
import org.dromara.hm.domain.bo.HmSaleOrderBo;
import org.dromara.hm.service.IHmSaleOrderService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 销售订单
 *
 * @author zy Hu
 * @date 2024-12-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/saleOrder")
public class HmSaleOrderController extends BaseController {

    private final IHmSaleOrderService hmSaleOrderService;
    private final IHmCustomerService customerService;
    private final IHmProductService productService;

    /**
     * 查询销售订单列表
     */
    @SaCheckPermission("hm:saleOrder:list")
    @GetMapping("/list")
    public TableDataInfo<HmSaleOrderVo> list(HmSaleOrderBo bo, PageQuery pageQuery) {
        return hmSaleOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出销售订单列表
     */
    @SaCheckPermission("hm:saleOrder:export")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmSaleOrderBo bo, HttpServletResponse response) {
        List<HmSaleOrderVo> list = hmSaleOrderService.queryList(bo);
        // 处理商品名称和客户名称
        list.forEach(item->{
            HmProductVo hmProductVo = productService.queryById(item.getSaleProductId());
            if (hmProductVo== null) {
                item.setSaleProductName("该商品已人为删除,ID为:"+item.getSaleProductId()+",请联系管理员");
                return;
            }
            item.setSaleProductName(hmProductVo.getProductName());
            HmCustomerVo hmCustomerVo = customerService.queryById(item.getCustomerId());
            if (hmCustomerVo == null) {
                item.setCustomerName("该客户已人为删除,ID为:"+item.getCustomerId()+",请联系管理员");
                return;
            }
            item.setCustomerName(hmCustomerVo.getCustomerName());

        });
        ExcelUtil.exportExcel(list, "销售订单", HmSaleOrderVo.class, response);
    }

    /**
     * 获取销售订单详细信息
     *
     * @param saleId 主键
     */
    @SaCheckPermission("hm:saleOrder:query")
    @GetMapping("/{saleId}")
    public R<HmSaleOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long saleId) {
        return R.ok(hmSaleOrderService.queryById(saleId));
    }

    /**
     * 新增销售订单
     */
    @SaCheckPermission("hm:saleOrder:add")
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmSaleOrderBo bo) {
        return toAjax(hmSaleOrderService.insertByBo(bo));
    }

    /**
     * 修改销售订单
     */
    @SaCheckPermission("hm:saleOrder:edit")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmSaleOrderBo bo) {
        return toAjax(hmSaleOrderService.updateByBo(bo));
    }

    /**
     * 订单退货
     */
    @Log(title = "订单退货", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/orderReturns")
    public R<Void> orderReturns( @RequestBody HmSaleOrderRefundBo bo) {
        return toAjax(hmSaleOrderService.orderReturns(bo));
    }

    /**
     * 删除销售订单
     *
     * @param saleIds 主键串
     */
    @SaCheckPermission("hm:saleOrder:remove")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{saleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] saleIds) {
        return toAjax(hmSaleOrderService.deleteWithValidByIds(List.of(saleIds), true));
    }
}
